//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.custom.flowtext;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;
import java.util.LinkedList;
import java.util.Random;
import java.util.Vector;

public class CFlowText extends FrameLayout implements OnGlobalLayoutListener {
    public static final int IDX_X = 0;
    public static final int IDX_Y = 1;
    public static final int IDX_TXT_LENGTH = 2;
    public static final int IDX_DIS_Y = 3;
    public static final int ANIMATION_IN = 1;
    public static final int ANIMATION_OUT = 2;
    public static final int OUTSIDE_TO_LOCATION = 1;
    public static final int LOCATION_TO_OUTSIDE = 2;
    public static final int CENTER_TO_LOCATION = 3;
    public static final int LOCATION_TO_CENTER = 4;
    public static final long ANIM_DURATION = 800L;
    public static final int MAX = 10;
    public static final int TEXT_SIZE_MAX = 25;
    public static final int TEXT_SIZE_MIN = 15;
    private OnClickListener itemClickListener;
    private static Interpolator interpolator;
    private static AlphaAnimation animAlpha2Opaque;
    private static AlphaAnimation animAlpha2Transparent;
    private static ScaleAnimation animScaleLarge2Normal;
    private static ScaleAnimation animScaleNormal2Large;
    private static ScaleAnimation animScaleZero2Normal;
    private static ScaleAnimation animScaleNormal2Zero;
    private Vector<String> vecKeywords;
    private int width;
    private int height;
    private boolean enableShow;
    private Random random;
    private int txtAnimInType;
    private int txtAnimOutType;
    private long lastStartAnimationTime;
    private long animDuration;

    public CFlowText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.init();
    }

    public CFlowText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public CFlowText(Context context) {
        super(context);
        this.init();
    }

    private void init() {
        this.lastStartAnimationTime = 0L;
        this.animDuration = 800L;
        this.random = new Random();
        this.vecKeywords = new Vector(10);
        this.getViewTreeObserver().addOnGlobalLayoutListener(this);
        interpolator = AnimationUtils.loadInterpolator(this.getContext(), 17432582);
        animAlpha2Opaque = new AlphaAnimation(0.0F, 1.0F);
        animAlpha2Transparent = new AlphaAnimation(1.0F, 0.0F);
        animScaleLarge2Normal = new ScaleAnimation(2.0F, 1.0F, 2.0F, 1.0F);
        animScaleNormal2Large = new ScaleAnimation(1.0F, 2.0F, 1.0F, 2.0F);
        animScaleZero2Normal = new ScaleAnimation(0.0F, 1.0F, 0.0F, 1.0F);
        animScaleNormal2Zero = new ScaleAnimation(1.0F, 0.0F, 1.0F, 0.0F);
    }

    public long getDuration() {
        return this.animDuration;
    }

    public void setDuration(long duration) {
        this.animDuration = duration;
    }

    public boolean feedKeyword(String keyword) {
        boolean result = false;
        if(this.vecKeywords.size() < 10) {
            result = this.vecKeywords.add(keyword);
        }

        return result;
    }

    public void feedKeyword(String[] keywords) {
        for(int i = 0; i < keywords.length; ++i) {
            if(this.vecKeywords.size() < 10) {
                this.vecKeywords.add(keywords[i]);
            }
        }

    }

    public boolean go2Show(int animType) {
        if(System.currentTimeMillis() - this.lastStartAnimationTime > this.animDuration) {
            this.enableShow = true;
            if(animType == 1) {
                this.txtAnimInType = 1;
                this.txtAnimOutType = 4;
            } else if(animType == 2) {
                this.txtAnimInType = 3;
                this.txtAnimOutType = 2;
            }

            this.disapper();
            boolean result = this.show();
            return result;
        } else {
            return false;
        }
    }

    private void disapper() {
        int size = this.getChildCount();

        for(int i = size - 1; i >= 0; --i) {
            final TextView txt = (TextView)this.getChildAt(i);
            if(txt.getVisibility() == 8) {
                this.removeView(txt);
            } else {
                LayoutParams layParams = (LayoutParams)txt.getLayoutParams();
                int[] xy = new int[]{layParams.leftMargin, layParams.topMargin, txt.getWidth()};
                AnimationSet animSet = this.getAnimationSet(xy, this.width >> 1, this.height >> 1, this.txtAnimOutType);
                txt.startAnimation(animSet);
                animSet.setAnimationListener(new AnimationListener() {
                    public void onAnimationStart(Animation animation) {
                    }

                    public void onAnimationRepeat(Animation animation) {
                    }

                    public void onAnimationEnd(Animation animation) {
                        txt.setOnClickListener((OnClickListener)null);
                        txt.setClickable(false);
                        txt.setVisibility(8);
                    }
                });
            }
        }

    }

    private boolean show() {
        if(this.width > 0 && this.height > 0 && this.vecKeywords != null && this.vecKeywords.size() > 0 && this.enableShow) {
            this.enableShow = false;
            this.lastStartAnimationTime = System.currentTimeMillis();
            int xCenter = this.width >> 1;
            int yCenter = this.height >> 1;
            int size = this.vecKeywords.size();
            int xItem = this.width / size;
            int yItem = this.height / size;
            LinkedList<Integer> listX = new LinkedList();
            LinkedList<Integer> listY = new LinkedList();

            for(int i = 0; i < size; ++i) {
                listX.add(Integer.valueOf(i * xItem));
                listY.add(Integer.valueOf(i * yItem + (yItem >> 2)));
            }

            LinkedList<TextView> listTxtTop = new LinkedList();
            LinkedList<TextView> listTxtBottom = new LinkedList();

            for(int i = 0; i < size; ++i) {
                String keyword = (String)this.vecKeywords.get(i);
                int ranColor = -16777216 | this.random.nextInt(7864319);
                int[] xy = this.randomXY(this.random, listX, listY, xItem);
                int txtSize = 15 + this.random.nextInt(11);
                TextView txt = new TextView(this.getContext());
                txt.setOnClickListener(this.itemClickListener);
                txt.setText(keyword);
                txt.setTextColor(ranColor);
                txt.setTextSize(2, (float)txtSize);
                txt.setShadowLayer(1.0F, 1.0F, 1.0F, -580294295);
                txt.setGravity(17);
                Paint paint = txt.getPaint();
                int strWidth = (int)Math.ceil((double)paint.measureText(keyword));
                xy[2] = strWidth;
                if(xy[0] + strWidth > this.width - (xItem >> 1)) {
                    int baseX = this.width - strWidth;
                    xy[0] = baseX - xItem + this.random.nextInt(xItem >> 1);
                } else if(xy[0] == 0) {
                    xy[0] = Math.max(this.random.nextInt(xItem), xItem / 3);
                }

                xy[3] = Math.abs(xy[1] - yCenter);
                txt.setTag(xy);
                if(xy[1] > yCenter) {
                    listTxtBottom.add(txt);
                } else {
                    listTxtTop.add(txt);
                }
            }

            this.attach2Screen(listTxtTop, xCenter, yCenter, yItem);
            this.attach2Screen(listTxtBottom, xCenter, yCenter, yItem);
            return true;
        } else {
            return false;
        }
    }

    private void attach2Screen(LinkedList<TextView> listTxt, int xCenter, int yCenter, int yItem) {
        int size = listTxt.size();
        this.sortXYList(listTxt, size);

        for(int i = 0; i < size; ++i) {
            TextView txt = (TextView)listTxt.get(i);
            int[] iXY = (int[])txt.getTag();
            int yDistance = iXY[1] - yCenter;
            int yMove = Math.abs(yDistance);

            int maxMove;
            int realMove;
            for(maxMove = i - 1; maxMove >= 0; --maxMove) {
                int[] kXY = (int[])((TextView)listTxt.get(maxMove)).getTag();
                realMove = kXY[0];
                int endX = realMove + kXY[2];
                if(yDistance * (kXY[1] - yCenter) > 0 && this.isXMixed(realMove, endX, iXY[0], iXY[0] + iXY[2])) {
                    int tmpMove = Math.abs(iXY[1] - kXY[1]);
                    if(tmpMove > yItem) {
                        yMove = tmpMove;
                    } else if(yMove > 0) {
                        yMove = 0;
                    }
                    break;
                }
            }

            if(yMove > yItem) {
                maxMove = yMove - yItem;
                int randomMove = this.random.nextInt(maxMove);
                realMove = Math.max(randomMove, maxMove >> 1) * yDistance / Math.abs(yDistance);
                iXY[1] -= realMove;
                iXY[3] = Math.abs(iXY[1] - yCenter);
                this.sortXYList(listTxt, i + 1);
            }

            LayoutParams layParams = new LayoutParams(-2, -2);
            layParams.gravity = 51;
            layParams.leftMargin = iXY[0];
            layParams.topMargin = iXY[1];
            this.addView(txt, layParams);
            AnimationSet animSet = this.getAnimationSet(iXY, xCenter, yCenter, this.txtAnimInType);
            txt.startAnimation(animSet);
        }

    }

    public AnimationSet getAnimationSet(int[] xy, int xCenter, int yCenter, int type) {
        AnimationSet animSet = new AnimationSet(true);
        animSet.setInterpolator(interpolator);
        TranslateAnimation translate;
        if(type == 1) {
            animSet.addAnimation(animAlpha2Opaque);
            animSet.addAnimation(animScaleLarge2Normal);
            translate = new TranslateAnimation((float)(xy[0] + (xy[2] >> 1) - xCenter << 1), 0.0F, (float)(xy[1] - yCenter << 1), 0.0F);
            animSet.addAnimation(translate);
        } else if(type == 2) {
            animSet.addAnimation(animAlpha2Transparent);
            animSet.addAnimation(animScaleNormal2Large);
            translate = new TranslateAnimation(0.0F, (float)(xy[0] + (xy[2] >> 1) - xCenter << 1), 0.0F, (float)(xy[1] - yCenter << 1));
            animSet.addAnimation(translate);
        } else if(type == 4) {
            animSet.addAnimation(animAlpha2Transparent);
            animSet.addAnimation(animScaleNormal2Zero);
            translate = new TranslateAnimation(0.0F, (float)(-xy[0] + xCenter), 0.0F, (float)(-xy[1] + yCenter));
            animSet.addAnimation(translate);
        } else if(type == 3) {
            animSet.addAnimation(animAlpha2Opaque);
            animSet.addAnimation(animScaleZero2Normal);
            translate = new TranslateAnimation((float)(-xy[0] + xCenter), 0.0F, (float)(-xy[1] + yCenter), 0.0F);
            animSet.addAnimation(translate);
        }

        animSet.setDuration(this.animDuration);
        return animSet;
    }

    private void sortXYList(LinkedList<TextView> listTxt, int endIdx) {
        for(int i = 0; i < endIdx; ++i) {
            for(int k = i + 1; k < endIdx; ++k) {
                if(((int[])((TextView)listTxt.get(k)).getTag())[3] < ((int[])((TextView)listTxt.get(i)).getTag())[3]) {
                    TextView iTmp = (TextView)listTxt.get(i);
                    TextView kTmp = (TextView)listTxt.get(k);
                    listTxt.set(i, kTmp);
                    listTxt.set(k, iTmp);
                }
            }
        }

    }

    private boolean isXMixed(int startA, int endA, int startB, int endB) {
        boolean result = false;
        if(startB >= startA && startB <= endA) {
            result = true;
        } else if(endB >= startA && endB <= endA) {
            result = true;
        } else if(startA >= startB && startA <= endB) {
            result = true;
        } else if(endA >= startB && endA <= endB) {
            result = true;
        }

        return result;
    }

    private int[] randomXY(Random ran, LinkedList<Integer> listX, LinkedList<Integer> listY, int xItem) {
        int[] arr = new int[]{((Integer)listX.remove(ran.nextInt(listX.size()))).intValue(), ((Integer)listY.remove(ran.nextInt(listY.size()))).intValue(), 0, 0};
        return arr;
    }

    public void onGlobalLayout() {
        int tmpW = this.getWidth();
        int tmpH = this.getHeight();
        if(this.width != tmpW || this.height != tmpH) {
            this.width = tmpW;
            this.height = tmpH;
            this.show();
        }

    }

    public Vector<String> getKeywords() {
        return this.vecKeywords;
    }

    public void rubKeywords() {
        this.vecKeywords.clear();
    }

    public void rubAllViews() {
        this.removeAllViews();
    }

    public void setOnItemClickListener(OnClickListener listener) {
        this.itemClickListener = listener;
    }
}
